<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Scala
 &#x26;  scala cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="本速查表可以用于快速地查找 Scala 语法结构


入门，为开发人员分享快速参考备忘单。">
<meta keywords="scala,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/scala.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.5"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="scala"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 32 32" height="1em" width="1em">
  <path d="M6.12 32c6.047 0 18.411-2.021 19.76-4v-7.641c-1.276 1.88-13.714 3.885-19.76 3.885zm0-10.182c6.047 0 18.411-2.021 19.76-4v-7.635c-1.276 1.875-13.714 3.88-19.76 3.88zm0-10.177c6.047 0 18.411-2.021 19.76-4V0C24.604 1.875 12.166 3.88 6.12 3.88z"></path>
</svg><a aria-hidden="true" tabindex="-1" href="#scala"><span class="icon icon-link"></span></a>Scala</h1><div class="wrap-body">
<p><a href="https://github.com/scala/scala/network/dependents"><img src="https://badgen.net/github/dependents-repo/scala/scala" alt="Repo Dependents"></a>
<a href="https://github.com/scala/scala"><img src="https://badgen.net/badge/icon/Github?icon=github&#x26;label" alt="Github repo"></a></p>
<p style="padding-top: 12px;">本速查表可以用于快速地查找 <a href="https://github.com/scala/scala">Scala</a> 语法结构</p>
<!--rehype:style=padding-top: 12px;-->
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#包">包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数据结构">数据结构</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#控制结构">控制结构</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件">条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件语法糖">条件（语法糖）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while-循环">while 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#do-while-循环">do while 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#break-slides">break. (slides)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-表达式-filtermap">for 表达式: filter/map</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-表达式-解构绑定">for 表达式: 解构绑定</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-表达式-叉乘">for 表达式: 叉乘</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-表达式-不可避免的格式">for 表达式: 不可避免的格式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-表达式-包括上边界的遍历">for 表达式: 包括上边界的遍历</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-表达式-忽略上边界的遍历">for 表达式: 忽略上边界的遍历</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#模式匹配">模式匹配</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在函数的参数中使用模式匹配">在函数的参数中使用模式匹配</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可以匹配任何int类型值的名称">可以匹配任何Int类型值的名称</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#面向对象">面向对象</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#构造器参数---私有">构造器参数 - 私有</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#构造器参数---公有">构造器参数 - 公有</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#构造函数就是类的主体">构造函数就是类的主体</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#匿名类">匿名类</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义一个抽象类">定义一个抽象类</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义一个继承子类">定义一个继承子类</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#继承与构造器参数">继承与构造器参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义一个单例">定义一个单例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#特质">特质</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多个特质">多个特质</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#必须声明覆盖该方法">必须声明覆盖该方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建对象">创建对象</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类字面量">类字面量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型检查-运行时">类型检查 (运行时)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型强制转换-运行时">类型强制转换 (运行时)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#归属-编译时">归属 (编译时)</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
<ul>
<li><a href="https://github.com/scala/scala">Scala</a> 官网  <em>(github.com)</em></li>
</ul>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-4"><div class="wrap-header h3wrap"><h3 id="函数"><a aria-hidden="true" tabindex="-1" href="#函数"><span class="icon icon-link"></span></a>函数</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-4-->
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">def</span> f<span class="token punctuation">(</span>x<span class="token operator">:</span> <span class="token builtin">Int</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span> x<span class="token operator">*</span>x <span class="token punctuation">}</span>  <span class="token comment">// ✅ GOOD</span>
</span><span class="code-line"><span class="token keyword">def</span> f<span class="token punctuation">(</span>x<span class="token operator">:</span> <span class="token builtin">Int</span><span class="token punctuation">)</span>   <span class="token punctuation">{</span> x<span class="token operator">*</span>x <span class="token punctuation">}</span>   <span class="token comment">// ❌ BAD 定义函数,潜在风险: </span>
</span><span class="code-line"><span class="token comment">// 不加“=”号将会是一段返回Unit类型的过程，</span>
</span><span class="code-line"><span class="token comment">// 这将会导致意想不到的错误。</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> f<span class="token punctuation">(</span>x<span class="token operator">:</span> <span class="token builtin">Any</span><span class="token punctuation">)</span> <span class="token operator">=</span> println<span class="token punctuation">(</span>x<span class="token punctuation">)</span>  <span class="token comment">// ✅ GOOD</span>
</span><span class="code-line"><span class="token keyword">def</span> f<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">=</span> println<span class="token punctuation">(</span>x<span class="token punctuation">)</span>       <span class="token comment">// ❌ BAD 定义函数 语法错误: 每个参数都需要指定类型。</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">type</span> R <span class="token operator">=</span> <span class="token builtin">Double</span>    <span class="token comment">// 类型别名</span>
</span><span class="code-line"><span class="token keyword">def</span> f<span class="token punctuation">(</span>x<span class="token operator">:</span> R<span class="token punctuation">)</span> vs<span class="token punctuation">.</span>
</span><span class="code-line"><span class="token keyword">def</span> f<span class="token punctuation">(</span>x<span class="token operator">:</span> <span class="token keyword">=></span> R<span class="token punctuation">)</span>     <span class="token comment">// 传值调用 传名调用 （惰性参数）</span>
</span><span class="code-line"><span class="token punctuation">(</span>x<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token keyword">=></span> x<span class="token operator">*</span>x       <span class="token comment">// 匿名函数</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span>map<span class="token punctuation">(</span>_<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">)</span> vs<span class="token punctuation">.</span>     <span class="token comment">// 匿名函数: 下划线是参数的占位符</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reduceLeft<span class="token punctuation">(</span> _<span class="token operator">+</span>_ <span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span>map<span class="token punctuation">(</span> x <span class="token keyword">=></span> x<span class="token operator">*</span>x <span class="token punctuation">)</span>  <span class="token comment">// 匿名函数: 必须命名以后才可以多次使用同一个参数</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span>map<span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token comment">// ✅ GOOD</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span>map<span class="token punctuation">(</span><span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// ❌ BAD 匿名函数: 绑定中缀方法，明智的做法是2*_。</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span>map <span class="token punctuation">{</span> x <span class="token keyword">=></span> <span class="token keyword">val</span> y<span class="token operator">=</span>x<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">;</span> println<span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> y <span class="token punctuation">}</span> <span class="token comment">// 匿名函数: 代码块风格，最后一个表达式作为返回值</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span> filter <span class="token punctuation">{</span>_<span class="token operator">%</span><span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">}</span> map <span class="token punctuation">{</span>_<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">}</span>           <span class="token comment">// 匿名函数: 管道风格（或者用圆括号）</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 匿名函数: 要传入多个代码块的话，需要使用花括号。</span>
</span><span class="code-line"><span class="token keyword">def</span> compose<span class="token punctuation">(</span>g<span class="token operator">:</span>R<span class="token keyword">=></span>R<span class="token punctuation">,</span> h<span class="token operator">:</span>R<span class="token keyword">=></span>R<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span>x<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token keyword">=></span> g<span class="token punctuation">(</span>h<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">val</span> f <span class="token operator">=</span> compose<span class="token punctuation">(</span><span class="token punctuation">{</span>_<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>_<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">val</span> zscore <span class="token operator">=</span> <span class="token punctuation">(</span>mean<span class="token operator">:</span>R<span class="token punctuation">,</span> sd<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token keyword">=></span> <span class="token punctuation">(</span>x<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token keyword">=></span> <span class="token punctuation">(</span>x<span class="token operator">-</span>mean<span class="token punctuation">)</span><span class="token operator">/</span>sd <span class="token comment">// 柯里化, 显然的语法。</span>
</span><span class="code-line"><span class="token keyword">def</span> zscore<span class="token punctuation">(</span>mean<span class="token operator">:</span>R<span class="token punctuation">,</span> sd<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span>x<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token keyword">=></span> <span class="token punctuation">(</span>x<span class="token operator">-</span>mean<span class="token punctuation">)</span><span class="token operator">/</span>sd <span class="token comment">// 柯里化, 显然的语法。</span>
</span><span class="code-line"><span class="token keyword">def</span> zscore<span class="token punctuation">(</span>mean<span class="token operator">:</span>R<span class="token punctuation">,</span> sd<span class="token operator">:</span>R<span class="token punctuation">)</span><span class="token punctuation">(</span>x<span class="token operator">:</span>R<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span>x<span class="token operator">-</span>mean<span class="token punctuation">)</span><span class="token operator">/</span>sd  <span class="token comment">// 柯里化，语法糖。然后:)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">val</span> normer <span class="token operator">=</span> zscore<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">0.4</span><span class="token punctuation">)</span> _  <span class="token comment">// 需要在尾部加下划线来变成偏函数（只对语法糖版本适用）</span>
</span><span class="code-line"><span class="token keyword">def</span> mapmake<span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">(</span>g<span class="token operator">:</span>T<span class="token keyword">=></span>T<span class="token punctuation">)</span><span class="token punctuation">(</span>seq<span class="token operator">:</span> List<span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">=</span> seq<span class="token punctuation">.</span>map<span class="token punctuation">(</span>g<span class="token punctuation">)</span> <span class="token comment">// 泛型</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token number">5.</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token number">5</span> <span class="token operator">+</span> <span class="token number">3</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span> map <span class="token punctuation">(</span>_<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 中缀语法糖</span>
</span><span class="code-line"><span class="token keyword">def</span> sum<span class="token punctuation">(</span>args<span class="token operator">:</span> <span class="token builtin">Int</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">=</span> args<span class="token punctuation">.</span>reduceLeft<span class="token punctuation">(</span>_<span class="token operator">+</span>_<span class="token punctuation">)</span> <span class="token comment">// 变长参数</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token number">5</span>         <span class="token comment">// 可变变量      </span>
</span><span class="code-line"><span class="token keyword">val</span> x <span class="token operator">=</span> <span class="token number">5</span>         <span class="token comment">// ✅ GOOD</span>
</span><span class="code-line">x<span class="token operator">=</span><span class="token number">6</span>               <span class="token comment">// ❌ BAD 常量</span>
</span><span class="code-line"><span class="token keyword">var</span> x<span class="token operator">:</span> <span class="token builtin">Double</span> <span class="token operator">=</span> <span class="token number">5</span> <span class="token comment">// 显式类型</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="包"><a aria-hidden="true" tabindex="-1" href="#包"><span class="icon icon-link"></span></a>包</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">import</span> <span class="token namespace">scala<span class="token punctuation">.</span>collection<span class="token punctuation">.</span></span>_ <span class="token comment">// 通配符导入</span>
</span><span class="code-line"><span class="token keyword">import</span> <span class="token namespace">scala<span class="token punctuation">.</span>collection<span class="token punctuation">.</span></span>Vector
</span><span class="code-line"><span class="token comment">// 选择性导入</span>
</span><span class="code-line"><span class="token keyword">import</span> <span class="token namespace">scala<span class="token punctuation">.</span>collection<span class="token punctuation">.</span></span><span class="token punctuation">{</span>Vector<span class="token punctuation">,</span> Sequence<span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 重命名导入</span>
</span><span class="code-line"><span class="token keyword">import</span> <span class="token namespace">scala<span class="token punctuation">.</span>collection<span class="token punctuation">.</span></span><span class="token punctuation">{</span>Vector <span class="token keyword">=></span> Vec28<span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 导入java.util包里除Date之外的一切</span>
</span><span class="code-line"><span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token punctuation">{</span>Date <span class="token keyword">=></span> _<span class="token punctuation">,</span> _<span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 文件开头的包名 pkg</span>
</span><span class="code-line"><span class="token keyword">package</span> <span class="token namespace">pkg</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment">// 声明这是一个包</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数据结构"><a aria-hidden="true" tabindex="-1" href="#数据结构"><span class="icon icon-link"></span></a>数据结构</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// 元组字面量 (Tuple3)</span>
</span><span class="code-line"><span class="token comment">// 解构绑定：通过模式匹配来解构元组。</span>
</span><span class="code-line"><span class="token keyword">var</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>z<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// ❌ BAD 潜在风险：整个元组被赋值给了每一个变量</span>
</span><span class="code-line"><span class="token keyword">var</span> x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>z <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 列表 (不可变)</span>
</span><span class="code-line"><span class="token keyword">var</span> xs <span class="token operator">=</span> List<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line">xs<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 用括号索引 (slides)</span>
</span><span class="code-line"><span class="token number">1</span> <span class="token operator">::</span> List<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// Cons（构成）</span>
</span><span class="code-line"><span class="token number">1</span> to <span class="token number">5</span> <span class="token comment">/* 等价于 => */</span> <span class="token number">1</span> until <span class="token number">6</span>
</span><span class="code-line"><span class="token comment">// Range类型（语法糖）</span>
</span><span class="code-line"><span class="token number">1</span> to <span class="token number">10</span> by <span class="token number">2</span>
</span><span class="code-line"><span class="token comment">// Unit类型的唯一成员 (相当于 C/Java 里的void)</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>空括号<span class="token punctuation">)</span> 
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="控制结构"><a aria-hidden="true" tabindex="-1" href="#控制结构"><span class="icon icon-link"></span></a>控制结构</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件"><a aria-hidden="true" tabindex="-1" href="#条件"><span class="icon icon-link"></span></a>条件</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>check<span class="token punctuation">)</span> happy <span class="token keyword">else</span> sad
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件语法糖"><a aria-hidden="true" tabindex="-1" href="#条件语法糖"><span class="icon icon-link"></span></a>条件（语法糖）</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>check<span class="token punctuation">)</span> happy same as
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>check<span class="token punctuation">)</span> happy <span class="token keyword">else</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while-循环"><a aria-hidden="true" tabindex="-1" href="#while-循环"><span class="icon icon-link"></span></a>while 循环</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">while</span> <span class="token punctuation">(</span>x <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> println<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> x <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="do-while-循环"><a aria-hidden="true" tabindex="-1" href="#do-while-循环"><span class="icon icon-link"></span></a>do while 循环</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">do</span> <span class="token punctuation">{</span> println<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> x <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>x <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="break-slides"><a aria-hidden="true" tabindex="-1" href="#break-slides"><span class="icon icon-link"></span></a>break. (slides)</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">import</span> <span class="token namespace">scala<span class="token punctuation">.</span>util<span class="token punctuation">.</span>control<span class="token punctuation">.</span></span>Breaks<span class="token punctuation">.</span>_
</span><span class="code-line">breakable <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token keyword">&#x3C;-</span> xs<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span>random <span class="token operator">&#x3C;</span> <span class="token number">0.1</span><span class="token punctuation">)</span> break
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-表达式-filtermap"><a aria-hidden="true" tabindex="-1" href="#for-表达式-filtermap"><span class="icon icon-link"></span></a>for 表达式: filter/map</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token keyword">&#x3C;-</span> xs <span class="token keyword">if</span> x<span class="token operator">%</span><span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">yield</span> x<span class="token operator">*</span><span class="token number">10</span>
</span><span class="code-line"><span class="token comment">// 等价于</span>
</span><span class="code-line">xs<span class="token punctuation">.</span>filter<span class="token punctuation">(</span>_<span class="token operator">%</span><span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>map<span class="token punctuation">(</span>_<span class="token operator">*</span><span class="token number">10</span><span class="token punctuation">)</span>         
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-表达式-解构绑定"><a aria-hidden="true" tabindex="-1" href="#for-表达式-解构绑定"><span class="icon icon-link"></span></a>for 表达式: 解构绑定</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> <span class="token keyword">&#x3C;-</span> xs zip ys<span class="token punctuation">)</span> <span class="token keyword">yield</span> x<span class="token operator">*</span>y
</span><span class="code-line"><span class="token comment">// 等价于</span>
</span><span class="code-line"><span class="token punctuation">(</span>xs zip ys<span class="token punctuation">)</span> map <span class="token punctuation">{</span> <span class="token keyword">case</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> <span class="token keyword">=></span> x<span class="token operator">*</span>y <span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-表达式-叉乘"><a aria-hidden="true" tabindex="-1" href="#for-表达式-叉乘"><span class="icon icon-link"></span></a>for 表达式: 叉乘</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token keyword">&#x3C;-</span> xs<span class="token punctuation">;</span> y <span class="token keyword">&#x3C;-</span> ys<span class="token punctuation">)</span> <span class="token keyword">yield</span> x<span class="token operator">*</span>y
</span><span class="code-line"><span class="token comment">// 等价于</span>
</span><span class="code-line">xs flatMap <span class="token punctuation">{</span>x <span class="token keyword">=></span> ys map <span class="token punctuation">{</span>y <span class="token keyword">=></span> x<span class="token operator">*</span>y<span class="token punctuation">}</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-表达式-不可避免的格式"><a aria-hidden="true" tabindex="-1" href="#for-表达式-不可避免的格式"><span class="icon icon-link"></span></a>for 表达式: 不可避免的格式</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span>x <span class="token keyword">&#x3C;-</span> xs<span class="token punctuation">;</span> y <span class="token keyword">&#x3C;-</span> ys<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">   println<span class="token punctuation">(</span><span class="token string">"%d/%d = %.1f"</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> x<span class="token operator">/</span>y<span class="token punctuation">.</span>toFloat<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>            
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-表达式-包括上边界的遍历"><a aria-hidden="true" tabindex="-1" href="#for-表达式-包括上边界的遍历"><span class="icon icon-link"></span></a>for 表达式: 包括上边界的遍历</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token keyword">&#x3C;-</span> <span class="token number">1</span> to <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  println<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-表达式-忽略上边界的遍历"><a aria-hidden="true" tabindex="-1" href="#for-表达式-忽略上边界的遍历"><span class="icon icon-link"></span></a>for 表达式: 忽略上边界的遍历</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token keyword">&#x3C;-</span> <span class="token number">1</span> until <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  println<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="模式匹配"><a aria-hidden="true" tabindex="-1" href="#模式匹配"><span class="icon icon-link"></span></a>模式匹配</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在函数的参数中使用模式匹配"><a aria-hidden="true" tabindex="-1" href="#在函数的参数中使用模式匹配"><span class="icon icon-link"></span></a>在函数的参数中使用模式匹配</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token comment">// ✅ GOOD</span>
</span><span class="code-line"><span class="token punctuation">(</span>xs zip ys<span class="token punctuation">)</span> map <span class="token punctuation">{</span> <span class="token keyword">case</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> <span class="token keyword">=></span> x<span class="token operator">*</span>y <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// ❌ BAD</span>
</span><span class="code-line"><span class="token punctuation">(</span>xs zip ys<span class="token punctuation">)</span> map<span class="token punctuation">(</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> <span class="token keyword">=></span> x<span class="token operator">*</span>y <span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="可以匹配任何int类型值的名称"><a aria-hidden="true" tabindex="-1" href="#可以匹配任何int类型值的名称"><span class="icon icon-link"></span></a>可以匹配任何Int类型值的名称</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">val</span> v42 <span class="token operator">=</span> <span class="token number">42</span>
</span><span class="code-line">Some<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">match</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> Some<span class="token punctuation">(</span>v42<span class="token punctuation">)</span> <span class="token keyword">=></span> println<span class="token punctuation">(</span><span class="token string">"42"</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> _ <span class="token keyword">=></span> println<span class="token punctuation">(</span><span class="token string">"Not 42"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>❌ BAD: “v42” 被解释为可以匹配任何Int类型值的名称，打印输出”42”</p>
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">val</span> v42 <span class="token operator">=</span> <span class="token number">42</span>
</span><span class="code-line">Some<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">match</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> Some<span class="token punctuation">(</span>`v42`<span class="token punctuation">)</span> <span class="token keyword">=></span> println<span class="token punctuation">(</span><span class="token string">"42"</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> _ <span class="token keyword">=></span> println<span class="token punctuation">(</span><span class="token string">"Not 42"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>✅ GOOD: 有反引号的 “<code>v42</code>” 被解释为已经存在的 <code>val v42</code>，所以输出的是 “Not 42”.</p>
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">val</span> UppercaseVal <span class="token operator">=</span> <span class="token number">42</span>
</span><span class="code-line">Some<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">match</span> <span class="token punctuation">{</span>
</span><span class="code-line"><span class="token keyword">case</span> Some<span class="token punctuation">(</span>UppercaseVal<span class="token punctuation">)</span> <span class="token keyword">=></span> println<span class="token punctuation">(</span><span class="token string">"42"</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> _ <span class="token keyword">=></span> println<span class="token punctuation">(</span><span class="token string">"Not 42"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>✅ GOOD: UppercaseVal 被视作已经存在的 val，而不是一个新的模式变量，因为它是以大写字母开头的，所以 <code>UppercaseVal</code> 所包含的值（42）和检查的值（3）不匹配，输出”Not 42”</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="面向对象"><a aria-hidden="true" tabindex="-1" href="#面向对象"><span class="icon icon-link"></span></a>面向对象</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="构造器参数---私有"><a aria-hidden="true" tabindex="-1" href="#构造器参数---私有"><span class="icon icon-link"></span></a>构造器参数 - 私有</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">class</span> C<span class="token punctuation">(</span>x<span class="token operator">:</span> R<span class="token punctuation">)</span> same as
</span><span class="code-line"><span class="token keyword">class</span> C<span class="token punctuation">(</span><span class="token keyword">private</span> <span class="token keyword">val</span> x<span class="token operator">:</span> R<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> C<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="构造器参数---公有"><a aria-hidden="true" tabindex="-1" href="#构造器参数---公有"><span class="icon icon-link"></span></a>构造器参数 - 公有</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">class</span> C<span class="token punctuation">(</span><span class="token keyword">val</span> x<span class="token operator">:</span> R<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token keyword">new</span> C<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
</span><span class="code-line">c<span class="token punctuation">.</span>x
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="构造函数就是类的主体"><a aria-hidden="true" tabindex="-1" href="#构造函数就是类的主体"><span class="icon icon-link"></span></a>构造函数就是类的主体</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">class</span> C<span class="token punctuation">(</span><span class="token keyword">var</span> x<span class="token operator">:</span> R<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  assert<span class="token punctuation">(</span>x <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"positive please"</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">var</span> y <span class="token operator">=</span> x
</span><span class="code-line">  <span class="token keyword">val</span> readonly <span class="token operator">=</span> <span class="token number">5</span>
</span><span class="code-line">  <span class="token keyword">private</span> <span class="token keyword">var</span> secret <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line">  <span class="token keyword">def</span> <span class="token keyword">this</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">(</span><span class="token number">42</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="匿名类"><a aria-hidden="true" tabindex="-1" href="#匿名类"><span class="icon icon-link"></span></a>匿名类</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">new</span><span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义一个抽象类"><a aria-hidden="true" tabindex="-1" href="#定义一个抽象类"><span class="icon icon-link"></span></a>定义一个抽象类</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">abstract</span> <span class="token keyword">class</span> D <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment">// 不可创建</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义一个继承子类"><a aria-hidden="true" tabindex="-1" href="#定义一个继承子类"><span class="icon icon-link"></span></a>定义一个继承子类</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">class</span> C <span class="token keyword">extends</span> D <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="继承与构造器参数"><a aria-hidden="true" tabindex="-1" href="#继承与构造器参数"><span class="icon icon-link"></span></a>继承与构造器参数</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">class</span> D<span class="token punctuation">(</span><span class="token keyword">var</span> x<span class="token operator">:</span> R<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">class</span> C<span class="token punctuation">(</span>x<span class="token operator">:</span> R<span class="token punctuation">)</span> <span class="token keyword">extends</span> D<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</span></code></pre>
<p>愿望清单: 默认自动传参</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义一个单例"><a aria-hidden="true" tabindex="-1" href="#定义一个单例"><span class="icon icon-link"></span></a>定义一个单例</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">object</span> O <span class="token keyword">extends</span> D <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
</span></code></pre>
<p>和模块一样</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="特质"><a aria-hidden="true" tabindex="-1" href="#特质"><span class="icon icon-link"></span></a>特质</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">trait</span> T <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">class</span> C <span class="token keyword">extends</span> T <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">class</span> C <span class="token keyword">extends</span> D <span class="token keyword">with</span> T <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>     
</span></code></pre>
<p>带有实现的接口，没有构造参数</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多个特质"><a aria-hidden="true" tabindex="-1" href="#多个特质"><span class="icon icon-link"></span></a>多个特质</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">trait</span> T1<span class="token punctuation">;</span> <span class="token keyword">trait</span> T2
</span><span class="code-line"><span class="token keyword">class</span> C <span class="token keyword">extends</span> T1 <span class="token keyword">with</span> T2
</span><span class="code-line"><span class="token keyword">class</span> C <span class="token keyword">extends</span> D <span class="token keyword">with</span> T1 <span class="token keyword">with</span> T2
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="必须声明覆盖该方法"><a aria-hidden="true" tabindex="-1" href="#必须声明覆盖该方法"><span class="icon icon-link"></span></a>必须声明覆盖该方法</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">class</span> C <span class="token keyword">extends</span> D <span class="token punctuation">{</span> <span class="token keyword">override</span> <span class="token keyword">def</span> f <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="创建对象"><a aria-hidden="true" tabindex="-1" href="#创建对象"><span class="icon icon-link"></span></a>创建对象</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line"><span class="token keyword">new</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">(</span><span class="token string">"f"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">new</span> List<span class="token punctuation">[</span><span class="token builtin">Int</span><span class="token punctuation">]</span>  <span class="token comment">// ✅ GOOD</span>
</span><span class="code-line">List<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>    <span class="token comment">// ❌ BAD</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类字面量"><a aria-hidden="true" tabindex="-1" href="#类字面量"><span class="icon icon-link"></span></a>类字面量</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line">classOf<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类型检查-运行时"><a aria-hidden="true" tabindex="-1" href="#类型检查-运行时"><span class="icon icon-link"></span></a>类型检查 (运行时)</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line">x<span class="token punctuation">.</span>isInstanceOf<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类型强制转换-运行时"><a aria-hidden="true" tabindex="-1" href="#类型强制转换-运行时"><span class="icon icon-link"></span></a>类型强制转换 (运行时)</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line">x<span class="token punctuation">.</span>asInstanceOf<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="归属-编译时"><a aria-hidden="true" tabindex="-1" href="#归属-编译时"><span class="icon icon-link"></span></a>归属 (编译时)</h3><div class="wrap-body">
<pre class="language-scala"><code class="language-scala code-highlight"><span class="code-line">x<span class="token operator">:</span> <span class="token builtin">String</span>
</span></code></pre>
</div></div></div></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.5.5" defer></script><script src="../js/fuse.min.js?v=1.5.5" defer></script><script src="../js/main.js?v=1.5.5" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
